package com.woniuxy01;


import java.util.ArrayList;
import java.util.function.Consumer;
import java.util.function.Function;
import java.util.function.Predicate;
import java.util.function.Supplier;

public class Test05 {
    public static void main(String[] args) {
        test05();
    }

    private static void test05() {
        Function<String,String> function1=new Function<String, String>() {
            @Override
            public String apply(String s) {
                return s.toUpperCase();
            }
        };
        System.out.println(function1.apply("aaaaaaa"));

        System.out.println(((Function<String, Integer>) str ->
                str.length()).apply("abcdefg"));
    }

    private static void test04() {
        Supplier<String> supplier1=new Supplier<String>() {
            @Override
            public String get() {
                return "aaaa";
            }
        };
        System.out.println(supplier1.get());
        StringBuilder sb = ((Supplier<StringBuilder>) () -> new StringBuilder("xxxx")).get();
        System.out.println(sb);
    }

    private static void test03() {
        Consumer<String> consumer1=new Consumer<String>(){
            @Override
            public void accept(String s) {
                System.out.println(s);
            }
        };
        consumer1.accept("xxxx");

        Consumer<String> consumer2=str-> System.out.println("输出结果:"+str);
        consumer2.accept("abc");

        ((Consumer)str-> System.out.println("本次输出结果:"+str)).accept("zzz");
    }

    private static void test02() {
        //判断字符是否满足某个条件:是否以"a"开头
        Predicate predicate1= new Predicate<String>(){
            @Override
            public boolean test(String s) {
                return s.startsWith("a");
            }
        };

        System.out.println(predicate1.test("abc"));
        System.out.println(predicate1.test("bca"));
        System.out.println(new Predicate<String>() {
            @Override
            public boolean test(String s) {
                return s.startsWith("a");
            }
        }.test("axxx"));

        Predicate<String> predicate2=str->str.length()>3;
        predicate2.test("abc");
        predicate2.test("abcd");
        ((Predicate<String>)str->str.contains("b")).test("abc");
        ((Predicate<String>)str->str.contains("b")).test("acb");
        ((Predicate<String>)str->str.contains("b")).test("acx");
    }

    private static void test01() {
        ArrayList<String> strs=new ArrayList();
        strs.add("aaa");
        strs.add("bbb");
        strs.add("ccc");
        //forEach(消费型接口：有参数，没有返回值)
        //展示数据，打印输出
        strs.forEach(new Consumer<String>() {
            @Override
            public void accept(String s) {
                System.out.println(s);
            }
        });
        strs.forEach( str->System.out.println(str));
    }
}
